package com.example.shopping_mall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.shopping_mall.R;
import com.example.shopping_mall.bean.CartBean;

import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyHolder>{
    private Context context;

    public CartAdapter(Context context) {
        this.context = context;
    }

    private List<CartBean> dataList;

    private onlistItenClickListener listener;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<CartBean> getDataList() {
        return dataList;
    }

    public void setDataList(List<CartBean> dataList) {
        this.dataList = dataList;
    }

    public onlistItenClickListener getListener() {
        return listener;
    }

    public void setListener(onlistItenClickListener listener) {
        this.listener = listener;
    }

    @Override
    public MyHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        return new MyHolder( LayoutInflater.from( context )
                .inflate( R.layout.shopping_mall_cart,null ) );
    }

    @Override
    public void onBindViewHolder( MyHolder holder, int position) {
        holder.icon.setImageResource( dataList.get( position ).getIcon() );
        holder.name.setText( dataList.get( position ).getName() );
        holder.color.setText( dataList.get( position ).getColor() );
        holder.price.setText( dataList.get( position ).getPrice() );
        holder.num.setText( dataList.get( position ).getNum() );

        holder.itemView.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onlistItenClick( position );
                }

            }
        } );

    }

    @Override
    public int getItemCount() {
        return null == dataList ? 0 :dataList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder{
        ImageView icon;
        TextView name;
        TextView color;
        TextView price;
        TextView num;
        TextView subtract;
        TextView add;

        public MyHolder( View itemView) {
            super( itemView );
            icon=itemView.findViewById( R.id.img );
            name=itemView.findViewById( R.id.good_name );
            color=itemView.findViewById( R.id.tv_good_color);
            price=itemView.findViewById( R.id.good_money );
            num=itemView.findViewById( R.id.num1 );
            subtract=itemView.findViewById( R.id.jian );
            add=itemView.findViewById( R.id.jia );

        }
    }

    public interface onlistItenClickListener {
        void onlistItenClick(int postion);
    }
}
